// packageA/member/presentationRecord_v2/presentationRecord_v2.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    language: '',
    selected: 0,
    //代打款
    waitPay: [],
    //打款
    pay: [],
    //待审核
    review: [],
    //无效数据
    invalid: [],
    //全部数据
    all: [],
    // loadMore
    loading: false,
    allLoaded: false,
    goload: false,
    orderType: "",
    ratioDatas: [],
    api: "",
    withdrawal_type: "",
    //more
    isLoadMore: true,
    page: 1,
    total_page: 0,
    name_of_withdrawal: ''
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    if (options.type) {
      this.data.withdrawal_type = options.type;
    }
    if (options.orderType) {
      this.setData({
        orderType: options.orderType
      });
    }
    this.customizeIncome();
    if (this.data.orderType == 'supplier') {
      if (options.member_id) {
        this.setData({
          member_id: options.member_id
        });
      }
      wx.setNavigationBarTitle({
        title: '我的' + this.data.name_of_withdrawal
      });
      this.getSupplierNetData();
    } else if (this.data.orderType === "extension") {
      this.data.api = "finance.withdraw.withdraw-log";
      let index = options.selectIndex || 0 ;
      this.setData({
        selected: index
      });
      this.getData(index);
    } else if(this.data.orderType === "storeBalance"){
      // 门店独立余额
      this.data.api = "plugin.store-cashier.frontend.store.balance.award.store";
      
      this.getBalanceData(this.selected);
    } else {
      this.setData({
        orderType: "store"
      });
      this.data.status = '3';
      this.getNetData(this.data.status);
    }
    if (this.data.orderType == 'store') {
      wx.setNavigationBarTitle({
        title: '我的提成'
      });
    } else {
      wx.setNavigationBarTitle({
        title: '我的' + this.data.name_of_withdrawal
      });
    }
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en});
  },
  getBalanceData(ind){
    let urlStr = app.getNetAddresss(this.data.api);
    app._getNetWork({
      url: urlStr,
      data:{
        status:ind
      },
      success: (resdata)=>{
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          wx.setNavigationBarTitle({
            title: '我的提成',
          });
          let mydata = res.data.list.data;
          let ratioDatas = [];
          //组装数据
          let itemDay = {};
          itemDay.money = res.data.statistic.today;
          itemDay.data = "今日";
          itemDay.name = "data";
          ratioDatas.push(itemDay);

          let itemYesterday = {};
          itemYesterday.money = res.data.statistic.yesterday;
          itemYesterday.data = "昨日";
          itemYesterday.name = "data";
          ratioDatas.push(itemYesterday);

          let itemWeek = {};
          itemWeek.money = res.data.statistic.week;
          itemWeek.data = "本周";
          itemWeek.name = "data";
          ratioDatas.push(itemWeek);

          let itemMonth = {};
          itemMonth.money = res.data.statistic.month;
          itemMonth.data = "本月累计提成";
          itemMonth.name = "mounth";
          ratioDatas.push(itemMonth);
          this.setData({
            ratioDatas: ratioDatas,
            allBalance: mydata,
            last_page:res.data.list.last_page,
          });
          console.log(this.data.ratioDatas);
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  getMoreBalanceData(){
    this.setData({
      page : this.data.page + 1
    });
    let urlStr = app.getNetAddresss(this.data.api);
    app._getNetWork({
      url: urlStr,
      data:{
        status:this.data.ind,
        page:this.data.page
      },
      success: (resdata)=>{
        wx.hideLoading({
          success: (res) => {},
        });
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          let mydata = res.data.list.data;
          this.data.allBalance = this.data.allBalance.concat(mydata);
          this.setData({
            allBalance: this.data.allBalance,
            last_page:res.data.list.last_page,
          });
          console.log(this.data.allBalance);
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        wx.hideLoading({
          success: (res) => {},
        });
        console.log(res);
      }
    });
  },
  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {

  },

  /**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function() {

  },

  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function() {

  },

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {
    if (this.data.isLoadMore) {
      console.log("加载更多");
      if (this.data.orderType === "supplier") {
        this.getMoreData1(this.data.page, this.data.selected);
      } else if (this.data.orderType === "extension") {
        this.getMoreData2(this.data.selected);
      } else if(this.data.orderType === "storeBalance"){
        if(this.data.page >= this.data.last_page){
          return;
        }else{
          wx.showLoading({
            title: '加载中',
          });
          this.getMoreBalanceData();
        }
      }else if (this.data.orderType === "store") {
        this.getMoreData3(this.data.status);
      }
    } else {
      console.log("没有更多数据");
    }
  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {

  },
  gopresentationDetails(e) {
    let id = e.currentTarget.dataset.recordid;
    wx.navigateTo({
      url: '/packageA/member/presentationDetails/presentationDetails?record_id=' + id
    });
  },
  //自定义提现收入语言
  customizeIncome() {
    try {
      var value = wx.getStorageSync('mailLanguage');
      if (value) {
        let mailLanguage = JSON.parse(value);
        // Do something with return value
        this.setData({
          name_of_withdrawal: mailLanguage.income.name_of_withdrawal
        });
      }
    } catch (e) {
      // Do something when catch error
    }
  },
  //供应商
  getSupplierNetData() {
    if (this.data.selected == 0) {
      //全部
      this.data.api = "plugin.supplier.frontend.withdraw-log.index";
    } else if (this.data.selected == 1) {
      //待审核
      this.data.api = "plugin.supplier.frontend.withdraw-log.applying";
    } else if (this.data.selected == 2) {
      //待打款
      this.data.api = "plugin.supplier.frontend.withdraw-log.pending";
    } else if (this.data.selected == 3) {
      //打款
      this.data.api = "plugin.supplier.frontend.withdraw-log.already";
    } else if (this.data.selected == 4) {
      //无效
      this.data.api = "plugin.supplier.frontend.withdraw-log.reject";
    }
    let that = this;
    let urlStr = app.getNetAddresss(that.data.api);
    urlStr += '&member_id=' + that.data.member_id;
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          var mydata = res.data;
          that.setData({
            total_page: mydata.total,
            allLoaded: false
          });
          if (!that.data.total_page) {
            that.setData({
              total_page: 0
            });
          }
          if (that.data.selected == 0) {
            // 全部
            that.setData({
              all: mydata.data
            });
          } else if (that.data.selected == 1) {
            // 待审核
            that.setData({
              review: mydata.data
            });
          } else if (that.data.selected == 2) {
            // 待打款
            that.setData({
              waitPay: mydata.data
            });
          } else if (that.data.selected == 3) {
            // 打款
            that.setData({
              pay: mydata.data
            });
          } else if (that.data.selected == 4) {
            // 无效
            that.setData({
              invalid: mydata.data
            });
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  getData(type) {
    let status = '';
    if (type == 0) {
      status = '';
    } else if (type == 1) {
      status = '0';
    } else if (type == 2) {
      status = '1';
    } else if (type == 3) {
      status = '2';
    } else if (type == 4) {
      status = '-1';
    }
    let that = this;
    let urlStr = app.getNetAddresss('finance.withdraw.withdraw-log');
    that.data.isLoadMore = false;
    that.data.page = 1;
    app._getNetWork({
      url: urlStr,
      data: {
        status: status
      },
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.data.allLoaded = false;
          that.data.isLoadMore = true;
          that.data.total_page = res.data.last_page;
          if (!that.data.total_page) {
            that.data.total_page = 0;
          }
          let mydata = res.data.data;
          if (that.data.selected == 0) {
            //全部
            that.setData({
              all: mydata
            });
          } else if (that.data.selected == 1) {
            //待审核
            that.setData({
              review: mydata
            });
          } else if (that.data.selected == 2) {
            //待打款
            that.setData({
              waitPay: mydata
            });
          } else if (that.data.selected == 3) {
            //打款
            that.setData({
              pay: mydata
            });
          } else if (that.data.selected == 4) {
            //无效
            that.setData({
              invalid: mydata
            });
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  getNetData(status) {
    const that = this;
    let urlStr = app.getNetAddresss('finance.withdraw.withdraw-list');
    that.data.isLoadMore = false;
    that.data.page = 1;
    app._getNetWork({
      url: urlStr,
      data: {
        status: status,
        withdrawal_type: this.data.withdrawal_type
      },
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.data.allLoaded = false;
          that.data.isLoadMore = true;
          that.data.total_page = res.data.last_page;
          if (!that.data.total_page) {
            that.data.total_page = 0;
          }
          let ratioDatas = [];
          //组装数据
          let itemDay = {};
          itemDay.money = res.data.today;
          itemDay.data = "今日";
          itemDay.name = "data";
          ratioDatas.push(itemDay);

          let itemYesterday = {};
          itemYesterday.money = res.data.yesterday;
          itemYesterday.data = "昨日";
          itemYesterday.name = "data";
          ratioDatas.push(itemYesterday);

          let itemWeek = {};
          itemWeek.money = res.data.lastweek;
          itemWeek.data = "本周";
          itemWeek.name = "data";
          ratioDatas.push(itemWeek);

          let itemMonth = {};
          itemMonth.money = res.data.thismonth;
          itemMonth.data = "本月累计提成";
          itemMonth.name = "mounth";
          ratioDatas.push(itemMonth);
          that.setData({
            ratioDatas: ratioDatas
          });
          var mydata = res.data.data;
          if (that.data.selected == 0) {
            //全部
            that.setData({
              all: mydata
            });
          } else if (that.data.selected == 1) {
            //待审核
            that.setData({
              review: mydata
            });
          } else if (that.data.selected == 2) {
            //待打款
            that.setData({
              waitPay: mydata
            });
          } else if (that.data.selected == 3) {
            //无效
            that.setData({
              invalid: mydata
            });
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  swichTabTItem(e) {
    let index = e.detail.index;
    this.setData({
      selected: index
    });
    if (this.data.orderType === "supplier") {
      this.getSupplierNetData();
    } else if (this.data.orderType === "extension") {
      this.getData(this.data.selected);
    } else if (this.data.orderType == 'storeBalance'){
      this.setData({
        allBalance:[],
        page:1,
        ind:e.detail.name
      });
      this.getBalanceData(this.data.ind);
    } else if (this.data.orderType === "store") {
      if (e.detail.index == 0) {
        this.data.status = '3';
      } else if (e.detail.index == 1) {
        this.data.status = '0';
      } else if (e.detail.index == 2) {
        this.data.status = '1';
      } else if (e.detail.index == 3) {
        this.data.status = '-1';
      }
      this.getNetData(this.data.status);
    }
  },
  getMoreData1(page, index) {
    var that = this;
    if (this.data.page == this.data.total_page) {
      return;
    }
    if (this.data.page >= this.data.total_page) {
      that.data.loading = true;
      that.data.allLoaded = true;
      return;
    } else {
      this.data.page = this.data.page + 1;
      let that = this;
      let urlStr = app.getNetAddresss(that.data.api);
      app._getNetWork({
        url: urlStr,
        data: {
          page: that.data.page,
          member_id: that.data.member_id
        },
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            var myData = res.data.data;
            that.data.loading = false;
            that.data.allLoaded = false;
            if (index == 0) {
              //全部
              that.setData({
                all: that.data.all.concat(myData) //数组拼接
              });
            } else if (index == 1) {
              //待审核
              that.setData({
                review: that.data.review.concat(myData) //数组拼接
              });
            } else if (index == 2) {
              //待打款
              that.setData({
                waitPay: that.data.waitPay.concat(myData)
              });
            } else if (index == 3) {
              //打款
              that.setData({
                pay: that.data.pay.concat(myData)
              });
            } else if (index == 4) {
              //无效
              that.setData({
                invalid: that.data.invalid.concat(myData)
              });
            }
          } else {
            that.data.page = that.data.page - 1;
            that.data.loading = true;
            that.data.allLoaded = true;
            that.data.isLoadMore = false;
          }
        },
        fail: function(res) {
          console.log(res);
        }
      });
    }
  },
  getMoreData2(type) {
    let status = '';
    if (type == 0) {
      status = '';
    } else if (type == 1) {
      status = '0';
    } else if (type == 2) {
      status = '1';
    } else if (type == 3) {
      status = '2';
    } else if (type == 4) {
      status = '-1';
    }
    let that = this;
    let urlStr = app.getNetAddresss('finance.withdraw.withdraw-log');
    that.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      app._getNetWork({
        url: urlStr,
        data: {
          status: status,
          page: that.data.page
        },
        success: function(resdata) {
          var res = resdata.data;
          that.data.isLoadMore = true;
          that.data.loading = false;
          that.data.allLoaded = false;
          if (res.result == 1) {
            let mydata = res.data.data;
            if (that.data.selected == 0) {
              //全部
              that.setData({
                all: that.all.concat(mydata)
              });
            } else if (that.data.selected == 1) {
              //待审核
              that.setData({
                review: that.review.concat(mydata)
              });
            } else if (that.data.selected == 2) {
              //待打款
              that.setData({
                waitPay: that.waitPay.concat(mydata)
              });
            } else if (that.data.selected == 3) {
              //打款
              that.setData({
                pay: that.pay.concat(mydata)
              });
            } else if (that.data.selected == 4) {
              //无效
              that.setData({
                invalid: that.invalid.concat(mydata)
              });
            }
          } else {
            that.data.page = that.data.page - 1;
            that.data.isLoadMore = false;
            that.data.loading = true;
            that.data.allLoaded = true;
            that.data.isLoadMore = false;
          }
        },
        fail: function(res) {
          console.log(res);
        }
      });
    }
  },
  getMoreData3(status) {
    let that = this;
    let urlStr = app.getNetAddresss('finance.withdraw.withdraw-log');
    that.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      app._getNetWork({
        url: urlStr,
        data: {
          status: that.data.status,
          withdrawal_type: that.data.withdrawal_type,
          page: that.data.page
        },
        success: function(resdata) {
          var res = resdata.data;
          that.data.isLoadMore = true;
          that.data.loading = false;
          that.data.allLoaded = false;
          if (res.result == 1) {
            let mydata = res.data.data;
            if (that.data.selected == 0) {
              //全部
              that.setData({
                all: that.all.concat(mydata)
              });
            } else if (that.data.selected == 1) {
              //待审核
              that.setData({
                review: that.review.concat(mydata)
              });
            } else if (that.data.selected == 2) {
              //待打款
              that.setData({
                waitPay: that.waitPay.concat(mydata)
              });
            } else if (that.data.selected == 3) {
              //无效
              that.setData({
                invalid: that.invalid.concat(mydata)
              });
            }
          } else {
            that.data.page = that.data.page - 1;
            that.data.isLoadMore = false;
            that.data.loading = true;
            that.data.allLoaded = true;
            that.data.isLoadMore = false;
          }
        },
        fail: function(res) {
          console.log(res);
        }
      });
    }
  }
});
